<?php
class Default_Model_LienHeMapper {

	protected $_dbTable;
	protected $db;

	public function setDbTable($dbTable) {
		if (is_string($dbTable)) {
			$dbTable = new $dbTable();
		}
		if (!$dbTable instanceof Zend_Db_Table_Abstract) {
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbTable = $dbTable;
		return $this;
	}

	public function getDbTable() {
		if (null === $this->_dbTable) {
			$this->setDbTable('Default_Model_DbTable_LienHe');
		}
		return $this->_dbTable;
	}
	
	public function getDb() {
		if ($this->db === null) {
			$this->db=Zend_Registry::get('connectDB');
		}
		return $this->db;
	}

	public function fetch($option ='view', $query = null) {
		
		$table = $this->getDbTable();
        $select = $table->select();
        $select->from($table, array('id', 'ten_cty', 'email', 'dia_chi', 'dien_thoai', 'fax'));
        
        if ($query != null)
        	$select->where($query);
        	
        $resultSet = $this->getDbTable()->fetchAll($select);
        
		return $this->processResultSet($resultSet->toArray(), $option);

	}
	
	/*
	 *processResultSet
	 */
	private function processResultSet($resultSet, $option = 'view') {
		$entries = "";
		if($option == 'view') {
			foreach ($resultSet as $row) {
				$entries = new Default_Model_TableColumn_LienHe($row);
			}
		} else {
			$entries = array();
			foreach ($resultSet as $row) {
				$entry = new Default_Model_TableColumn_LienHe($row);
				$entries[] = $entry;
			}
		}
		return $entries;
	}
}